package psi.controllers;

import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Record;

import psi.common.PSIException;
import psi.common.Result;
import psi.services.UserService;

/**
 * 用户 Controller
 * 
 * @author 李静波
 *
 */
public class UserController extends PSIBaseController {

	/**
	 * 用户登录页面
	 */
	public void login() {
		initVarsForView();
		
		// "1" : 使用 http://pv.sohu.com/cityjson?ie=utf-8 来获取客户端IP地址
		// 原因：部署在MoPaaS V2上的时候，因为在容器内，无法直接获得客户端IP地址
		setAttr("useSohuPV", "1"); 

		setAttr("title", "登录");
		setAttr("demoInfo", new UserService().getDemoInfo());

		render("login.html");
	}

	/**
	 * 执行用户登录操作
	 */
	@Before(POST.class)
	public void loginPOST() {
		String ip = getPara("ip");
		if (ip == null || ip.equals("")) {
			ip = getRequest().getRemoteAddr();
		}
		
		setSessionAttr("login_user_ip", ip);

		String loginName = getPara("loginName");
		String password = getPara("password");
		Result result = new UserService().doLogin(loginName, password);

		renderJson(result);
	}

	/**
	 * 用户管理 - 主页面
	 */
	public void index() {
		initVarsForView();

		setAttr("title", "用户管理");

		render("index.html");
	}

	/**
	 * 返回所有的组织机构
	 */
	@Before(POST.class)
	public void allOrgs() {
		renderJson(new UserService().allOrgs());
	}

	@Before(POST.class)
	public void users() {
		Record params = new Record();
		params.set("orgId", getPara("orgId"));
		params.set("start", getPara("start"));
		params.set("limit", getPara("limit"));

		renderJson(new UserService().users(params));
	}

	@Before(POST.class)
	public void editOrg() {
		Record params = new Record();
		params.set("id", getPara("id"));
		params.set("orgCode", getPara("orgCode"));
		params.set("parentId", getPara("parentId"));
		params.set("name", getPara("name"));

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.editOrg(params));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}

	@Before(POST.class)
	public void orgParentName() {
		String id = getPara("id");

		renderJson(new UserService().orgParentName(id));
	}

	@Before(POST.class)
	public void deleteOrg() {
		String id = getPara("id");

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.deleteOrg(id));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}

	@Before(POST.class)
	public void editUser() {
		Record params = new Record();
		params.set("id", getPara("id"));
		params.set("loginName", getPara("loginName"));
		params.set("name", getPara("name"));
		params.set("orgCode", getPara("orgCode"));
		params.set("orgId", getPara("orgId"));
		params.set("birthday", getPara("birthday"));
		params.set("idCardNumber", getPara("idCardNumber"));
		params.set("tel", getPara("tel"));
		params.set("tel02", getPara("tel02"));
		params.set("address", getPara("address"));
		params.set("gender", getPara("gender"));
		params.set("enabled", getPara("enabled"));

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.editUser(params));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}

	@Before(POST.class)
	public void deleteUser() {
		String id = getPara("id");

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.deleteUser(id));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}

	@Before(POST.class)
	public void changePassword() {
		String id = getPara("id");
		String password = getPara("password");

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.changePassword(id, password));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}

	/**
	 * 修改我的密码 - 主页面
	 */
	public void changeMyPassword() {
		initVarsForView();

		UserService us = new UserService();
		setAttr("loginUserId", us.getLoginUserId());
		setAttr("loginName", us.getLoingName());

		setAttr("title", "修改我的密码");

		render("changeMyPassword.html");

	}

	@Before(POST.class)
	public void changeMyPasswordPOST() {
		String userId = getPara("userId");
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");

		UserService userService = enhance(UserService.class);

		try {
			renderJson(userService.changeMyPassword(userId, oldPassword, newPassword));
		} catch (PSIException e) {
			renderJson(bad(e.getMessage()));
		}
	}
}
